#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdbool.h>
typedef struct Student {
    long long num;
    char name[100];
    int age;
    int class;
} stu;

//链表
typedef struct Node {
    stu student;
    struct Node* next;
} node;

node* phead = NULL; //头指针

int menu() {
    printf("+------------------------+\n");
    printf("+----学生资源管理系统----+\n");
    printf("+-----请选择功能---------+\n");
    printf("+------1录入学生信息-----+\n");
    printf("+------2显示学生信息-----+\n");
    printf("+------3保存学生信息-----+\n");
    printf("+----4修改添加学生信息---+\n");
    printf("+------5删除学生信息-----+\n");
    printf("+------6查找学生信息-----+\n");
    printf("+------0退出系统---------+\n");
    printf("+------------------------+\n");
    int chose;
    scanf("%d", &chose);
    return chose;
}

void entry(node** ph) {
    node* newnode = (node*)malloc(sizeof(node));
    if (newnode == NULL) {
        printf("内存分配失败\n");
        return;
    }
    newnode->next = NULL;
    printf("输入学号\n");
    scanf("%llu", &newnode->student.num);
    printf("输入名字\n");
    scanf("%s", newnode->student.name, sizeof(newnode->student.name));
    printf("输入年龄\n");
    scanf("%d", &newnode->student.age);
    printf("输入班级\n");
    scanf("%d", &newnode->student.class);
    if (*ph == NULL) {
        *ph = newnode;
    }
    else {
        node* tail = *ph;
        while (tail->next != NULL) {
            tail = tail->next;
        }
        tail->next = newnode;
    }
}

void print(node *ph) {
       node* cur = ph;
        if (cur==NULL){
    	printf("无");	 
	}
       
    if (cur==NULL){
    	printf("无");	 
	}
    while (cur != NULL) {
        printf("学号%llu  名字%s  年龄%d  班级%d\n", cur->student.num, cur->student.name, cur->student.age, cur->student.class);
        cur = cur->next;
    }

}

void save(node* ph) {
    FILE* fp = fopen("students_data.txt", "w");
    if (!fp) {
        perror("文件打开失败");
        return;
    }
    node* cur = ph;
    while (cur != NULL) {
        fprintf(fp, "%llu\t%s\t%d\t%d\n", cur->student.num, cur->student.name, cur->student.age, cur->student.class);
        cur = cur->next;
    }
    fclose(fp);
}
node* search(node* ph) {
    printf("输入名字");
    char a[50];
    scanf("%s", a);
    node* cur = ph;
    while (cur != NULL) {
        if (strcmp(cur->student.name,a) == 0) {
            return cur;
        }
        cur = cur->next;
    }
    if(cur==NULL){
    return NULL;	;
	}
}
void alter(node *ph){
	        printf("输入名字\n");
    char a[50];
    scanf("%s", a);
    node* cur = ph;
    while (cur != NULL) {
        if (strcmp(cur->student.name,a) == 0) {
            printf("输入要修改的学生的学号 名字 年龄 班级");
            scanf("%llu %s %d %d",&cur->student.num, &cur->student.name,& cur->student.age,& cur->student.class);
        }
        cur = cur->next;
    }
    
}
void delete (node **ph){
	printf("输入名字");
    char a[50];
    scanf("%s", a);
    node* cur = *ph;
    node *copy=NULL;
    while (cur != NULL) {
        if (strcmp(cur->student.name,a) == 0) {
                  break;
        }
        copy=cur;
        cur = cur->next;
}
if(cur!=NULL){
	//删的是第一节点
	if(copy==NULL){
	*ph=cur->next;	
	}
	 else{
	 	   copy->next=cur->next;
	 }
	   free(cur);
}
}


int main() {
    while (true) {
    	system("pause");
        system("cls");
        switch (menu()) {
        case 0:
            printf("退出系统...\n");
            return 0;
        case 1:
            entry(&phead);
            break;
        case 2:
            print(phead);
            break;
        case 3:
            save(phead);
            break;
        case 4:
             alter(phead);
            break;
        case 5:
            delete(&phead);
            break;
        case 6: {
            node* cur = search(phead);
            if (!cur) {
                printf("未找到该学生");
            }
            else {
                printf("学号%llu  名字%s  年龄%d  班级%d\n", cur->student.num, cur->student.name, cur->student.age, cur->student.class);
            }
            break;
        }
        }
}
    return 0;
}